<?php

require_once('./discuz_publish.php');
set_time_limit(0);

function get_url($url, $base_url='')
{
	$content = file_get_contents($url);
	file_put_contents( dirname(__FILE__ ) . '/log', $content);
	//exit(0);
	preg_match_all('/<li><span class="start"><img src="Images\/3star.gif" \/>.*?<\/a><\/li>/', $content, $matches);
	$matches = $matches[0];
	if(is_array($matches))
	{
		foreach($matches as $v)
		{
			$v = substr($v, stripos($v, "href='")+strlen("href='"));
			$href = $base_url . substr($v, 0, stripos($v, "'"));

			$v = substr($v, stripos($v, 'title="')+strlen('title="'));
			$title = substr($v, 0, stripos($v, '"'));
			$result[] = array('href'=>$href, 'title'=>$title);
		}
	}
	return $result;
	//file_put_contents( dirname(__FILE__ ) . '/log', var_export($result, true));
}

$base_url = 'http://www.jhwulifudao.com/';
$urls = array('http://www.jhwulifudao.com/DatumList-62.aspx');

if($_GET['i'] == 1)
{
	$urls = array('http://www.jhwulifudao.com/DatumList-62_2.aspx', 'http://www.jhwulifudao.com/DatumList-62_3.aspx', 'http://www.jhwulifudao.com/DatumList-62_4.aspx', 'http://www.jhwulifudao.com/DatumList-62_5.aspx');
}
else if($_GET['i'] == 2)
{
	$urls = array('http://www.jhwulifudao.com/DatumList-61.aspx', 'http://www.jhwulifudao.com/DatumList-61_2.aspx', 'http://www.jhwulifudao.com/DatumList-61_3.aspx', 'http://www.jhwulifudao.com/DatumList-61_4.aspx', 'http://www.jhwulifudao.com/DatumList-61_5.aspx', 'http://www.jhwulifudao.com/DatumList-61_6.aspx');	
}
else if($_GET['i'] == 3)
{
	$urls = array('http://www.jhwulifudao.com/DatumList-71.aspx');
	for($i=2; $i<16; $i++)
	{
		$urls[] = "http://www.jhwulifudao.com/DatumList-71_$i.aspx";
	}
	//'http://www.enmajor.com/cn/Html/Listening_Speaking/Listening/index_2.html','http://www.enmajor.com/cn/Html/Listening_Speaking/Listening/index_3.html');
}
else
{
	$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/jokes/');
}

$i = 0;
foreach($urls as $url)
{
	$result = get_url($url, $base_url);
	foreach($result as $v)
	{
		$i++;
		//if($i < 10) continue;
		get_content($v['href'], $v['title']);
	
		if($i == 500) exit(0);
	}
}

exit(0);

function get_content($href, $title, $g_url='http://www.jhwulifudao.com/')
{
	$base_url = $href;
	$content = file_get_contents($base_url);
	file_put_contents( dirname(__FILE__ ) . '/log', $content . '{br}');//exit(0);
	$i = 0;

	$content = file_get_contents($base_url);
	
	$pos = stripos($content, '<div id="infoconter">');
	$content = substr($content, $pos + strlen('<div id="infoconter">'));
	/*
	if( $pos == false)
	{
		$content = substr($content, stripos($content, '<table border="0" cellspacing="5" cellpadding="5" width="100%">') + strlen('<table border="0" cellspacing="5" cellpadding="5" width="100%">'));
	}*/
	$cont = substr($content, 0, stripos($content, '<div id="downloadbutton">'));
		
	if(empty($title) || empty($cont)) break;
	
	$content = substr($content, stripos($content, '<div id="downloadbutton">'));
	$down_href = substr($content, stripos($content, '<a href="') + strlen('<a href="'));
	$down_href = substr($down_href, 0, stripos($down_href, '"'));
	$down_href = $g_url . $down_href;

	$cont .= "下载链接 :" . $down_href;
	file_put_contents( dirname(__FILE__ ) . '/log', $title . '{br}'. $cont . '{br}');
//exit(0);
	discuz_publish(escape_s($title), escape_s($cont), 79);
//EXIT(0);
	//file_put_contents( dirname(__FILE__ ) . '/log', escape_s($title) . '{br}'. escape_s($cont) . '{br}', FILE_APPEND);
	//exit(0);
}

function escape_s($content)
{
	$content = str_replace( "'", "\'", $content);
	$content = str_replace('"', '\"', $content);
	$content = str_replace('<br />', '', $content);
	$content = preg_replace('/<div.*?>/i', '', $content);
	$content = preg_replace('/<\/div>/i', '', $content);
	$content = preg_replace('/<p>/i', "\r\n", $content);
	return $content;
}
//var_dump($next);




?>